import Vue from 'vue'

import Vuex from 'vuex'
import createPs from 'vuex-persistedstate'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
		//公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		addlist:[],
		
		// 预订单
		loadOrder:'',
		
		// 订单列表
		orderList:[],
		// 当前用户信息
		user:''
	},
    mutations: {
		//相当于同步的操作
		setAdd(state,data){
			state.addlist.push(data)
			uni.setStorage({
				key: 'addList',
				data: state.addlist,
				success: function () {
					console.log('success');
				}
			});
		},
		getData(state,data){
			let index = data.findIndex(item => item.isDefault==true)
			let mr =''
			mr = data[0]
			data[0] = data[index]
			data[index] = mr
			uni.setStorage({
				key: 'addList',
				data: data,
				success: function () {
					console.log('success');
				}
			});
			state.addlist = data
		},
		addLoadOrder(state,data){
			console.log(data);
			state.loadOrder = data
		},
		
		setOrder(state,data){
			switch(data.type){
				case 'add':
				state.orderList.push(data.data)
				break
			}
			// console.log(data);
		},
		
		setUser(state,data){
			state.user = data
		}
	},
    actions: {
		//相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
	},
	plugins:[createPs({
		storage:window.localStorage
	})]
})
export default store
